cmake_minimum_required(VERSION 3.5)
project(modbus)

project(modbus LANGUAGES C CXX)

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)


if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE "Release")
endif()

set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wall -g")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -g")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")

find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
find_package(agv_msgs REQUIRED)
find_package(agv_srvs REQUIRED)
find_package(shared_parameters REQUIRED)

find_package(Boost REQUIRED COMPONENTS thread system chrono)

include_directories(
  include
  include/modbus
)

add_library(modbus SHARED
  src/modbus_communication.cpp
  src/modbus_slave.cpp
  src/modbus_master.cpp
  src/modbus-data.c       # C源文件
  src/modbus-rtu.c        # C源文件
  src/modbus-tcp.c        # C源文件
  src/modbus.c            # C源文件
)

set_source_files_properties(
  src/modbus-data.c
  src/modbus-rtu.c
  src/modbus-tcp.c
  src/modbus.c
  PROPERTIES LANGUAGE C
)

target_link_libraries(modbus
  ${rclcpp_LIBRARIES}
  ${std_msgs_LIBRARIES}
)

ament_target_dependencies(modbus
  rclcpp
  std_msgs
  agv_msgs
  agv_srvs
  shared_parameters
)

add_executable(master_node 
  test/master_manager.cpp
  test/manager_node.cpp
)

target_link_libraries(master_node
  modbus
  ${rclcpp_LIBRARIES}
)

ament_target_dependencies(master_node
  rclcpp
  std_msgs
  agv_msgs
  agv_srvs
  shared_parameters
)

install(TARGETS master_node
  RUNTIME DESTINATION lib/${PROJECT_NAME}
)

install(TARGETS
  modbus
  master_node
  ARCHIVE DESTINATION lib
  LIBRARY DESTINATION lib
  RUNTIME DESTINATION bin
)

install(DIRECTORY include/
  DESTINATION include
)

install(DIRECTORY launch/
  DESTINATION share/${PROJECT_NAME}/launch
)

ament_export_include_directories(include)
ament_export_libraries(modbus)
ament_export_dependencies(
  rclcpp
  std_msgs
  agv_msgs
  agv_srvs
)

ament_package()